<?php
include('../fns_all.php');

function all_topics_for_forum($forumID){
	$cxn = db_connect();
	$sql = "SELECT * FROM forums_topics WHERE parent_forum = ".$forumID;
	return mysqli_query($cxn,$sql);
}

function get_threads_count_for_topic($topic_id){
	$cxn = db_connect();
	$query   = "SELECT COUNT(id) AS numthreads FROM forums_threads where parent_topic = '$topic_id'";
	$result  = mysqli_query($cxn, $query) or die('Error, get_threads_count_for_topic failed');
	$row     = mysqli_fetch_assoc($result);
	return $row['numthreads'];
}

function all_forums(){
	$cxn = db_connect();
	return mysqli_query($cxn,"SELECT * FROM forums");
}

function all_threads_for_topic2($id){
	$cxn = db_connect();
	$query = "SELECT Thread.*, date, author
	FROM forums_threads as Thread, forums_posts
	WHERE
	parent_topic =" . $id . " AND
	parent_thread = Thread.id AND
	in_reply_to IS NULL";
	return mysqli_query( $cxn, $query );
}

function all_threads_for_topic($id){
	$cxn = db_connect();
	$query = "SELECT *
	FROM forums_threads
	WHERE
	parent_topic =" . $id . " ";
	return mysqli_query( $cxn, $query );
}

function get_thread($id){
	$cxn = db_connect();
	$query = "SELECT *
	FROM forums_threads
	WHERE
	id =" . $id . " ";
	return mysqli_query( $cxn, $query );
}

function get_num_posts_for_user($user){
	if($user != 'Unregistered User'){
		$cxn = db_connect();
		$query = "SELECT *
		FROM forums_posts
		WHERE author = '". $user."'";
		$result = mysqli_query( $cxn, $query );
		$num_rows = mysqli_num_rows($result);
		return $num_rows;
	}else{
		return 1;
	}
}

function all_messages_for_thread($threadID){
	$cxn = db_connect();
	$query = "SELECT id, author, date, body
	FROM forums_posts
	WHERE parent_thread = ". $threadID;
	return mysqli_query( $cxn, $query );
}

function get_author_details($author){
	$cxn = db_connect();
	$query = "SELECT *
	FROM users
	WHERE user_name = '". $author."'";
	return mysqli_query( $cxn, $query );
}

function get_parent_thread($freplyto){
	$cxn = db_connect();
	// Find the parent_thread of the message that the user
	// is replying to
	$query = "SELECT parent_thread FROM forums_posts
	WHERE id = " . $freplyto;
	return mysqli_query( $cxn, $query );
}

function reply_to_message($parent_thread, $freplyto, $author, $body){
	$cxn = db_connect();
	// And add the new message to the Post table
	$query = "INSERT INTO forums_posts
	(parent_thread, in_reply_to, author, body) ";
	$query .= "VALUES( ";
	$query .= $parent_thread . ",";
	$query .= $freplyto . ",";
	$query .= "'$author',";
	$query .= "'$body')";
	
	logToFile($query);
	
	return mysqli_query( $cxn, $query );
}

function update_thread_after_adding_message($parent_thread){
	$cxn = db_connect();
	$query = "SELECT replies FROM forums_threads
			WHERE id = $parent_thread";
			$result = mysqli_query($cxn,$query);
			$reps = mysqli_fetch_assoc($result);
			$query = "UPDATE  forums_threads SET last_post = now(),
			replies = {$reps['replies']}+1
			WHERE id = $parent_thread";
	return mysqli_query( $cxn, $query );
}

function get_message_details($replyTo){
	$cxn = db_connect();
	$query = "SELECT Post.author, date, body, subject
	FROM forums_posts as Post,forums_threads as Thread
	WHERE Post.id = " . $replyTo . "
	AND Thread.id = Post.parent_thread";
	$result = mysqli_query( $cxn, $query );
	return $result;
}

function create_new_thread($ftopicID, $author, $subject){
	$cxn = db_connect();
	$sql = "INSERT INTO forums_threads(parent_topic, author,subject) ";
	$sql .= "VALUES( ";
	$sql .= $ftopicID . ", ";
	$sql .= "'" . $author . "','";
	$sql .= $subject ."')";
	if(!mysqli_query($cxn, $sql))
	{
		echo "Error: ".mysqli_error($cxn);
	}
	return mysqli_insert_id($cxn);
}

function add_new_message($parentThread, $author, $body){
	$cxn = db_connect();
	$author = mysqli_real_escape_string($cxn, $author);
	$body = mysqli_real_escape_string($cxn, $body );
	$query = "INSERT INTO forums_posts (parent_thread,author,body)";
	$query .= "VALUES( ";
	$query .= $parentThread . ",";
	$query .= "'" . $author . "',";
	$query .= "'" . $body. "')";
	return mysqli_query( $cxn, $query );
}

function get_name_for_topic($parentTopic){
	$cxn = db_connect();
	$sql = "SELECT name FROM forums_topics WHERE id = $parentTopic";
	return mysqli_query($cxn, $sql);
}

function insert_forum($name, $author){
	$cxn = db_connect();
	return mysqli_query ($cxn, "INSERT INTO forums (name, author) VALUES ('$name', '$author')");
}

function insert_topic($id, $name, $author){
	$cxn = db_connect();
	return mysqli_query ($cxn, "INSERT INTO forums_topics (parent_forum, name, author) VALUES ('$id','$name', '$author')");
}

function delete_forum($id){
	$cxn = db_connect();
	return mysqli_query ($cxn, "delete from forums where id = '$id'");
}

function delete_topic($id){
	$cxn = db_connect();
	return mysqli_query ($cxn, "delete from forums_topics where id = '$id'");
}

function delete_thread($id){
	$cxn = db_connect();
	return mysqli_query ($cxn, "delete from forums_threads where id = '$id'");
}

function delete_post($id){
	$cxn = db_connect();
	return mysqli_query ($cxn, "delete from forums_posts where id = '$id'");
}
?>